import type { RoleItem, RolePermissionItem } from './model/rolesModel'

import type { ApiPagination, ApiPaginationParams } from '#types/utils'

import { requestClient } from '../request'

// 角色管理
export default class SysRolesApi {
    static get inst() {
        return SysRolesApi._inst || (SysRolesApi._inst = new SysRolesApi())
    }
    private static _inst: SysRolesApi
    /**
     * 新增
     * @param params
     */
    add(params: Partial<RoleItem>) {
        return requestClient.post<null>('/role/add', params)
    }
    /**
     * 批量删除
     * @param ids
     */
    delete(ids: Array<number | string>) {
        return requestClient.delete<null>(`/role/delete/${ids}`)
    }

    /**
     * 编辑
     * @param id
     * @param params
     */
    edit(id: number | string, params: Partial<RoleItem>) {
        return requestClient.put<null>(`/role/edit/${id}`, params)
    }

    /**
     * 获取全部角色
     */
    getAllList() {
        return requestClient.get<RoleItem[]>('/role/getRoleList')
    }

    /**
     * 获取权限列表
     * @param id
     */
    getMenuList(id: number | string) {
        return requestClient.get<RolePermissionItem[]>(`/role/getMenuList/${id}`)
    }
    /**
     * 列表分页
     * @param params
     */
    getPageList(params: ApiPaginationParams<{ name?: string }>) {
        return requestClient.get<ApiPagination<RoleItem>>('/role/index', {
            params
        })
    }

    /**
     * 保存权限
     * @param id
     * @param menuIds
     */
    savePermission(id: number | string, menuIds: number[]) {
        return requestClient.put<RolePermissionItem[]>(`/role/savePermission/${id}`, menuIds)
    }
}
